package com.sline.com.sline.Synchronized;

public class Demo2_ThreadSafe {
    public static void main(String[] args) {
//        new Ticket().start();
//        new Ticket().start();
//        new Ticket().start();
//        new Ticket().start();

        Ticket1 ti1 = new Ticket1();
        new Thread(ti1).start();
        new Thread(ti1).start();
        new Thread(ti1).start();
        new Thread(ti1).start();
    }
}

class Ticket extends Thread {
    private static int ticket = 1000;

    public void run() {
        while (true) {
            synchronized (Ticket.class) {
                if (ticket <= 0) {
                    break;
                } else {
                    System.out.println(this.getName() + " 这是第" + ticket-- + "号票");
                }
            }
        }
    }
}

class Ticket1 implements Runnable {
    private int ticket = 1000;
    @Override
    public void run() {
        while (true) {
            synchronized (Ticket1.class) {
                if (ticket <= 0) {
                    break;
                } else {
                    System.out.println(Thread.currentThread().getName() + " 这是第" + ticket-- + "号票");
                }
            }
        }
    }
}
